<?php

/**
 * Generate UUID
 * @return	string	- UUID key
 */
function gen_uuid() {
    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}

/**
 * Generate unique ID
 * @param integer $len	- ID length
 * @return string		- ID
 */
function gen_uid($len) {
    $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $out = '';

    for ($i=0; $i<$len; $i++) {
        $out .= $str[mt_rand(0, 61)];
    }
    return $out;
}
/**
 * Encrypt string with AES
 *
 * @param unknown_type $str
 * @param unknown_type $key
 */
function encrypt_string($str, $key) {
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_256,
                          md5($key),
                          $str,
                          MCRYPT_MODE_ECB,
                          mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB),
                                                              MCRYPT_RAND
                                          )
                          );
}

/**
 * Decrypt AES string
 *
 * @param unknown_type $str
 * @param unknown_type $key
 */
function decrypt_string($str, $key) {
    return trim(
                mcrypt_decrypt(MCRYPT_RIJNDAEL_256,
                               md5($key),
                               $str,
                               MCRYPT_MODE_ECB,
                               mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB),
                                                                   MCRYPT_RAND
                                               )
                               )
               );
}

?>
